﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Text.RegularExpressions;
using Com.CMS263.Util;
using System.Xml.Linq;
using System.Xml;
namespace Com.CMS263.BLL
{
    public class BasePage : System.Web.UI.Page
    {

        private List<string> tagList = new List<string>();
        TagUtil tu = new TagUtil();
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            string content = string.Empty;
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            try
            {
                // 将当前页面的内容呈现到临时的 HtmlTextWriter 对象中   
                base.Render(htmlWriter);
                htmlWriter.Close();
                // 得到当前页面的全部内容   
                content = stringWriter.ToString();
                string tagClass= System.Configuration.ConfigurationSettings.AppSettings["tags"];
                // 替换页面中的部分内容  
                //ArtListTransform alt = new ArtListTransform(content);
                //content = alt.GetResulte();
                //ArtPageTransform apt = new ArtPageTransform(content);
                //content = apt.GetResulte();
                //ArtCateTransform act = new ArtCateTransform(content);
                //content = act.GetResulte();
                List<string> tagClasslist = new List<string>();
                tagClasslist=tagClass.Split(',').ToList<string>();
                foreach (string tag in tagClasslist)
                {
                   content= ReflactUtil.InvokeMethod(tag, "Replace", new Type[] { typeof(string) }, new object[] { content }).ToString();
                }


                // 将新页面的内容显示出来   
                writer.Write(content);
            }
            catch { }
            finally
            {
                stringWriter.Dispose();
                htmlWriter.Close();
                htmlWriter.Dispose();
            }


        }

        /// <summary>
        /// 得到指定标签所包含的内容
        /// </summary>
        /// <param name="ctx">包含标签的内容</param>
        /// <param name="tagName">标签的名字不含"<>"</param>
        /// <returns></returns>
        private void GetTag(string ctx, string tagName)
        {
            if (ctx.IndexOf("<" + tagName) >= 0)
            {
                int start = ctx.IndexOf("<" + tagName);
                int end = ctx.IndexOf("</" + tagName + ">") + tagName.Length + 3;

                tagList.Add(ctx.Substring(start, end - start));

                string subHtml = ctx.Substring(end + tagName.Length + 3);
                if (subHtml.IndexOf("<" + tagName) >= 0)
                {
                    GetTag(subHtml, tagName);
                }
            }


        }
        /// <summary>
        /// 解析cms:list标签
        /// </summary>
        /// <param name="tagHtml">包含html待解析的标签</param>
        /// <returns>解析后的内容</returns>
        private string TransformArtListTag(string tagHtml)
        {
            int end = tagHtml.IndexOf(">") + 1;
            string attrs = tagHtml.Substring(0, end);
            List<string> attrList = new List<string>();
            Dictionary<string, string> attr = new Dictionary<string, string>();
            string[] arr = attrs.Split(' ');
            //得到所有属性的字符串
            foreach (string str in arr)
            {
                if (str.Replace(" ", "") != "")
                {
                    attrList.Add(str);
                }
            }
            //拆分属性字符串，得到键值对
            foreach (string str in attrList)
            {
                if (str.Split('=').Length > 1)
                {
                    attr[str.Split('=')[0]] = str.Split('=')[1];
                }
            }

            return "";
        }


    }
}
